package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.Order;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.OrderService;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;


    @GetMapping("/{travellerId}/{tourGroupId}")
    public Result<Order> getById(@PathVariable Long travellerId, @PathVariable Long tourGroupId) {
        Order order = orderService.getByTravellerIdAndTourGroupId(travellerId, tourGroupId);
        if (order != null) {
            return Result.ok(order);
        }
        return Result.error("获取失败");
    }

    @PostMapping
    public Result<Void> save(@RequestBody Order order) {
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        boolean isSuccess = orderService.save(order);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.error("保存失败");
    }

    @PutMapping
    public Result<Void> update(@RequestBody Order order) {
        order.setUpdateTime(LocalDateTime.now());
        boolean isSuccess = orderService.lambdaUpdate().eq(Order::getTravellerId, order.getTravellerId())
                .eq(Order::getTourGroupId, order.getTourGroupId()).update(order);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @PutMapping("/updateOrderStatusById/{id}/{status}")
    public Result<Void> updateOrderStatusById(@PathVariable Long id, @PathVariable Integer status) {
        boolean isSuccess = this.orderService.updateStatusById(id,status);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.error("更新订单状态失败");
    }

    @DeleteMapping("/{travellerId}/{tourGroupId}")
    public Result<Void> deleteByTravellerIdAndTourGroupId(@PathVariable Long travellerId, @PathVariable Long tourGroupId) {
        orderService.removeByTravellerIdAndTourGroupId(travellerId, tourGroupId);
        return Result.ok();
    }


}
